/**
 * @Date: 2022-04-09 16:36:59
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-07-15 20:52:26
 * @FilePath: \weibo-api\controller\utils.js
 * @Description: 杂七杂八
 */
const path = require("path");
const { SuccessModel, ErrorModel } = require("../ResModel/index");
const fse = require("fs-extra");
const upload = async (ctx) => {
  const file = ctx.request.files.file;
  const basename = path.basename(file.path);
  const size = file.size / 1024 / 1024;
  // console.log(size);
  if (size > 0.8) {
    await fse.remove(file.path); //文件过大删除
    return new ErrorModel(413, "上传图片超过0.8M");
  }
  // console.log("图片地址", basename, `${ctx.origin}/uploads/${basename}`);
  return new SuccessModel({
    url: `${ctx.origin}/uploads/${basename}`,
  });
};
module.exports = {
  upload,
};
